Skip to main content

Workflow

  • Workflow Definition/Workflow Function
    • A normal Typescript function that defines the workflow steps by step
  • Deterministic constraints
    • The same command are always emitted in the workflow given an input
    • When a Workflow Function executes, it emits a Command which will then be compared with the Event History, if the Event already exist and if the emitted Command matches with what already executed (this means a Replay), the execution progresses, (returns a non-deterministic error instead)
    • Code change can lead to non-deterministic error => Use Workflow versioning

You do not handle Worker Process failure or restarts in a Workflow Definition.

Workflow Function Executions are completely oblivious to the Worker Process in terms of failures or downtime. The Temporal Platform ensures that the state of a Workflow Execution is recovered and progress resumes if there is an outage of either Worker Processes or the Temporal Cluster itself. The only reason a Workflow Execution might fail is due to the code throwing an error or exception, not because of underlying infrastructure outages.

  • Workflow Execution

    • Created by executing Workflow Function
    • Executes concurrently to all other Workflow Execution and communicate with each other through Signals and the environment through Activities
  • Command

  • Event

    • Events are created by the Temporal Cluster in response to external occurrences and Commands generated by a Workflow Execution. Each Event corresponds to an enum that is defined in the Server API.
    • All Events are recorded in the Event History.
  • **Signal

    • A Signal is an asynchronous request to a Workflow Execution.
    • A Signal delivers data to a running Workflow Execution. It cannot return data to the caller; to do so, use a Query instead.
  • Query

Activities

  • The temporal workflow function is a collection of normal typescript operations and temporal activities
  • Activities are allowed to be non-deterministic and must be where non-deterministic parts of a temporal workflow are written (example simple activity). Some examples of non-deterministic operations: retrieving the current time (Date.now()), generating a random number, and fetching a record from the database.
  • However, the activity result is recorded in the workflow execution history. So during recovery, the result is taken from the history without executing the activity. This means that even if the activity is non-deterministic, its result is recorded and used in the replay, ensuring the workflow remains deterministic.
  • For example, consider the following workflow:
void workflowMethod(int param) {
int result = activity1(param)

activity2(result)
}
  • When activity1 completes, Temporal stores its result in the internal database. This means that it won't call that activity again, so the code in that activity can do anything.
    • So we can think of activities as cached function, which is just regular function but being wrapped around Temporal activities
int activty1(int param) {
return new Random().nextInt()
}
  • But you can't do something like this:
void workflowMethod(int param) {
int result = activity1(param)
int test = new Random().nextInt()
// no deterministic error here
activity2(result, test)
Workflow.sleep(1minute)
}
  • In this case, Temporal will reexecute the workflow methods with cached results from events. It will schedule activity1 and get the result from cache, next it will generate a random number again (it wasn't cached), for example, it will be equal to 6. Next, it will schedule activity2 with params anything and 6, and validation will fail because params should be anything and 4, that's why a NoDeterministic error would be thrown5^.

https://community.temporal.io/t/why-cant-workflows-contain-non-deterministic-code-and-how-does-using-activity-solve-the-problem/3231/7 https://community.temporal.io/t/workflow-with-a-state/6851

Worker